<template>
  <div class="app-container">
    <!-- 添加或修改工单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body v-drag-fullscreen>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="form-flex">
        <fieldset>
          <legend>基础信息</legend>
          <el-row :gutter="20">
            <el-col :span="6" :xs="24">
              <el-form-item label="所属小区" prop="communityId">
                <el-select v-model="form.communityId" placeholder="请选择所属小区" @change="getBuildingData"
                           :disabled=disabled>
                  <el-option
                    v-for="dict in communityOptions"
                    :key="dict.id"
                    :label="dict.communityName"
                    :value="dict.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6" :xs="24">
              <el-form-item label="所属楼栋" prop="floorId">
                <el-select v-model="form.floorId" placeholder="请选择所属楼栋" @change="getUnitList" :disabled=disabled>
                  <el-option
                    v-for="item in floorOptions"
                    :key="item.id"
                    :label="item.floorName"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6" :xs="24">
              <el-form-item label="所属单元" prop="unitId">
                <el-select v-model="form.unitId" placeholder="请选择所属单元" @change="getRoomListData" :disabled=disabled>
                  <el-option
                    v-for="item in unitOptions"
                    :key="item.id"
                    :label="item.unitName"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6" :xs="24">
              <el-form-item label="所属房屋" prop="roomId">
                <el-select v-model="form.roomId" placeholder="请选择所属房屋" @change="changeRoom" :disabled=disabled>
                  <el-option
                    v-for="item in roomOptions"
                    :key="item.id"
                    :label="item.roomNum"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12" :xs="24">
              <el-form-item label="所在位置" prop="location">
                <el-input v-model="form.location" placeholder="请输入所在位置" :disabled="disabled"/>
              </el-form-item>
            </el-col>
            <el-col :span="6" :xs="24">
              <el-form-item label="服务区域">
                <el-radio-group v-model="form.coverage" :disabled="disabled">
                  <el-radio
                    v-for="item in dict.coverage"
                    :key="item.dictValue"
                    :label="item.dictValue"
                  >{{item.dictLabel}}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </fieldset>

        <fieldset>
          <legend>报修信息</legend>
          <el-row>
            <el-col :span="6" :xs="24">
              <el-form-item label="报修项目" prop="warrantyProject">
                <treeselect v-model="form.warrantyProject" :disabled="disabled" :options="warrantyProjectOptions" :disable-branch-nodes="false" placeholder="选择报修项目" />
              </el-form-item>
            </el-col>
            <el-col :span="6" :xs="24">
              <el-form-item label="维修类型" prop="repairType">
                <el-select v-model="form.repairType" placeholder="请选择维修类型" :disabled="disabled">
                  <el-option
                    v-for="item in dict.service_type"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6" :xs="24">
              <el-form-item label="重要程度" prop="degreeImportance">
                <el-select v-model="form.degreeImportance" placeholder="请选择重要程度" :disabled="disabled">
                  <el-option
                    v-for="item in dict.degree_importance"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12" :xs="24">
              <el-form-item label="内容描述" prop="content">
                <el-input v-model="form.content" type="textarea"  :autosize="{ minRows: 4, maxRows: 6}" placeholder="请输入内容" :disabled="disabled"/>
              </el-form-item>
            </el-col>
            <el-col :span="12" :xs="24">
              <el-form-item label="图片" prop="imageAddress">
                <image-uploads ref="imageAddressRefs" :disabled="disabled" :fileList="fileList" :limit="3"/>
              </el-form-item>
            </el-col>
          </el-row>
        </fieldset>
        <fieldset>
          <legend>其他信息</legend>
          <el-row>
            <el-col :span="6" :xs="24">
              <el-form-item label="发起人" prop="charterMemberName">
                <el-input v-model="form.charterMemberName" placeholder="请输入发起人名称" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="6" :xs="24">
              <el-form-item label="发起时间" prop="charterTime">
                <el-date-picker clearable size="small" :disabled="disabled"
                                v-model="form.charterTime"
                                type="datetime"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                placeholder="选择发起时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6" :xs="24">
              <el-form-item label="反映人" prop="reflectPeople">
                <el-input v-model="form.reflectPeople" placeholder="请输入反映人" :disabled="disabled"/>
              </el-form-item>
            </el-col>
            <el-col :span="6" :xs="24">
              <el-form-item label="手机号" prop="phoneNum">
                <el-input v-model="form.phoneNum" placeholder="请输入内容" :disabled="disabled"/>
              </el-form-item>
            </el-col>
            <el-col :span="6" :xs="24">
              <el-form-item label="接单人" prop="receivingPersonName">
                <el-input v-model="form.receivingPersonName"  placeholder="请选择接单人" readonly >
                  <el-button slot="append" v-if="!disabled" icon="el-icon-search"  @click="()=>this.$refs.receivingRef.show()" ></el-button>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6" :xs="24">
              <el-form-item label="预约时间" prop="appointmentTime">
                <el-date-picker clearable size="small" :disabled="disabled"
                                v-model="form.appointmentTime"
                                type="datetime"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                placeholder="选择预约时间"
                                @change="changeAppointmentTime"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>

          </el-row>
        </fieldset>
        <fieldset v-if="disabled">
          <legend>工单轨迹</legend>
          <work-order-track ref="trackRefs" :workOrderId="form.id"/>
        </fieldset>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="!disabled" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">{{disabled?'关闭':'取 消'}}</el-button>
      </div>
    </el-dialog>

    <TableSelect
      ref="receivingRef"
      :listColumns="receivingColumns"
      gridUrl="/system/user/list"
      @rowClick="receivingSelect"
      dialogWidth="780px"
      title="用户字典"
    />

  </div>
</template>

<script>
  import {getWorkOrder, addWorkOrder, updateWorkOrder} from "@/api/workorder/workOrder";
  import { workOrderMixin } from "@/views/workorder/workOrder/workOrderMixin.js"
  import WorkOrderTrack from "./workOrderTrack"

  export default {
    name: "reportRepairForm",
    dicts: ['coverage', 'degree_importance', 'service_type', 'complaints_type', 'project_identification', 'work_order_status',],
    mixins: [workOrderMixin],
    components:{ WorkOrderTrack },
    data() {
      return {
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //是否禁用
        disabled: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          phoneNum: [
            {required: true, message: "手机号不能为空", trigger: "blur"},
            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              message: "请输入正确的手机号码",
              trigger: "blur"
            }
          ],
          reflectPeople: [
            {required: true, message: "反映人不能为空", trigger: "blur"}
          ],
          communityId: [
            {required: true, message: "所属小区不能为空", trigger: "blur"}
          ],
          degreeImportance: [
            {required: true, message: "重要程度不能为空", trigger: "blur"}
          ],
          warrantyProject: [
            {required: true, message: "报修项目不能为空", trigger: "blur"}
          ],
          repairType: [
            {required: true, message: "维修类型不能为空", trigger: "blur"}
          ],
          charterMemberName: [
            {required: true, message: "发起人不能为空", trigger: "blur"}
          ],
          charterTime: [
            {required: true, message: "发起时间不能为空", trigger: "blur"}
          ],
          location: [
            {required: true, message: "所在位置不能为空", trigger: "blur"}
          ],
          content: [
            {required: true, message: "内容描述不能为空", trigger: "blur"}
          ],
        },
        receivingColumns:[
          {title:'用户名称',key:'nickName',type:'',isSearch:true},
          {title:'部门',key:'dept.deptName',type:'',isSearch:false},
          {title:'手机号码',key:'phonenumber',type:'',isSearch:true},
        ],
        fileList:[]

      };
    },
    created() {
      this.getWarrantyProject()
    },
    methods: {
      show(option) {
        this.reset();
        this.form.orderType = option.orderType;
        let typeName = option.typeName || '报事报修'
        this.form.charterMemberName = this.$store.state.user.nickName;
        this.disabled = option.type == 'VIEW'
        if (option.type == 'ADD') {
          this.open = true;
          this.title = `新增${typeName}`;
          this.form.charterTime = this.parseTime(new Date())
          this.form.appointmentTime= this.parseTime(new Date())
          this.initHouseInfo()
        } else if (option.type == 'UPDATE' || option.type == 'VIEW') {
          getWorkOrder(option.id).then(response => {
            this.form = response.data;
            this.initHouseInfo()
            this.fileList = []
            if(this.form.imageAddress && this.form.imageAddress.length > 0){
              let imageAddress = this.form.imageAddress.split(",")
              for(let i in imageAddress){
                this.fileList.push({name:imageAddress[i],url:imageAddress[i]})
              }
            }
            if (option.type == 'VIEW') {
              this.title = `查看${typeName}`;
            } else {
              this.title = `修改${typeName}`;
            }
            this.open = true;
          });
        }
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
        this.$emit("cancel")
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          communityId: null,
          floorId: null,
          unitId: null,
          roomId: null,
          no: null,
          orderType: null,
          charterMemberId: null,
          charterMemberName: null,
          charterTime: null,
          coverage: "10",
          degreeImportance: null,
          location: null,
          reflectPeopleId: null,
          reflectPeople: null,
          phoneNum: null,
          content: null,
          warrantyProject: null,
          repairType: null,
          complaintType: null,
          projectIdentification: null,
          imageAddress: null,
          receivingPersonId: null,
          receivingPersonName: null,
          jointlyPersonId: null,
          jointlyPersonName: null,
          appointmentTime: null,
          suspendPersonId: null,
          suspendPersonName: null,
          status: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          wordType:'30',
        };
        this.resetForm("form");
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateWorkOrder(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.cancel()
              });
            } else {
              addWorkOrder(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.cancel()
              });
            }
          }
        });
      },
      receivingSelect(index,row){
        this.form.receivingPersonId= row.userId;
        this.form.receivingPersonName=row.nickName;
      },
      changeAppointmentTime(v){
        if(v <= this.form.charterTime){
          this.msgWarning("预约时间必须小于发起时间。")
          this.form.appointmentTime = null
        }
      }
    }
  };
</script>
